﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MODEL;

namespace DAL
{
    public class ClassesDAL
    {
        public void Dr2Class(DataRow row, ClassModel model)
        {
            if (row.Table.Columns.Contains("CID") && row["CID"].ToString() != "")
            {
                model.CID = (int)row["CID"];
            }
            if (row.Table.Columns.Contains("CID") && row["CName"].ToString() != "")
            {
                model.CName = (string)row["CName"];
            }
            if (row.Table.Columns.Contains("CID") && row["CCount"].ToString() != "")
            {
                model.CCount = (int)row["CCount"];
            }
            if (row.Table.Columns.Contains("CID") && row["CIsDel"].ToString() != "")
            {
                model.CIsDel = (bool)row["CIsDel"];
            }
            if (row.Table.Columns.Contains("CID") && row["CAddtime"].ToString() != "")
            {
                model.CAddtime = (DateTime)row["CAddtime"];
            }
            
           
           
        }

        private SqlHelper sqlhelper = new SqlHelper();

        public List<ClassModel> GetAllClass()
        {
            DataTable table = sqlhelper.ExecuteDataTable("select * from Classes");
            if (table.Rows.Count <= 0)
            {
                return null;
            }
            else
            {
                List<ClassModel> list = new List<ClassModel>();
                foreach (DataRow  row in table.Rows)
                {
                    ClassModel model = new ClassModel();
                    Dr2Class(row, model);
                    list.Add(model);
                }
                return list;
            }
        }
    }
}
